<?php 
namespace Home\Model;
use Think\Model;

class UserModel extends Model{

//登录密码验证
public function  checkPwd($password){
  
      $realpass=$this->password;

      $this->password=$password;

 	    $this->encRypt();

   	return $realpass === $this->password;
}

//发放Cookie
public function auth(){
   cookie('user_id',$this->user_id);
   cookie('user',$this->username);
   cookie('pwd',$this->password);
}



//---------------注册验证----------------------//

//自动验证,注册表单信息
	protected $_validate=array(
	//array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
		array('username','/\w{4,12}/','用户名4-12位','1','regex',1),
		array('username',' ','用户名几存在','1','unique',1),
		array('email','email','email不合法',1,'regex',1),
		array('email',' ','email几存在',1,'unique',1),
		array('password','6,16','密码在6-16位',1,'length',3),
		array('password2','password','2次密码不一致',1,'confirm',3),

	);

//自动完成
    protected $_auto = array(
        // array(完成字段1,完成规则,[完成条件,附加规则]),
        array('addtime',	'time',	  1,   'function'),
        array('lastupdate' , 'time' , 2 , 'function'),
    );

 //随机字符串
    public function randStr(){
    $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
     return(substr(str_shuffle($str),0,8));
    }
 
//生成盐
   public function salt(){
   	if(!isset($this->salt)){
   		$this->salt=$this->randStr();
   	}
   	return $this->salt;
  }

//md5加密
   protected function encRypt(){
   	$this->salt();
   	return $this->password=md5($this->password.$this->salt);
   }

//调用其它的方法，进行md5加密和加盐
public function reg(){
	$this->encRypt();
	return $this->add();
}


}





 ?>